package com.wudeyong.util;

import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Author :wudeyong
 * Date 2017/6/23
 * For More Information,Please Visit https://wudeyong.com
 */
public class HttpGet {



    //  模拟谷歌浏览器代理
    private String userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
    private String method="GET";
//  支持的编码
    private String acceptEncoding="gzip";
    private String acceptLanguage="zh-CN";
    private String cacheControl="max-age=0";
    private String accept="text/html;charset=utf-8";
    private String cookie="";

    private Map makeHeader(){
        Map<String,String> headerMap= new HashMap<String,String>();
        headerMap.put(parseString("userAgent"),userAgent);
        headerMap.put(parseString("method"),method);
        headerMap.put(parseString("acceptEncoding"),acceptEncoding);
        headerMap.put(parseString("acceptLanguage"),acceptLanguage);
        headerMap.put(parseString("cacheControl"),cacheControl);
        headerMap.put(parseString("accept"),accept);
        headerMap.put(parseString("cookie"),cookie);
        return headerMap;
    }


    public void getResultString(InputStream inputStream){
        BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
        try {
            String tmp="";
            while ((tmp=reader.readLine())!=null)
                System.out.println(tmp);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public InputStream linkUrl(String url){
        try {
            URL netUrl=new URL(url);
            TrustManager []manager={new MyTrustManager()};
            SSLContext sslContext=SSLContext.getInstance("SSLv3");
            sslContext.init(null,manager,new SecureRandom());
//            构造SSL工厂
            SSLSocketFactory sslSocketFactory=sslContext.getSocketFactory();
//            构建httpURLConnection对象
            HttpsURLConnection connection=(HttpsURLConnection)netUrl.openConnection();
            connection=addHeader(connection);
            connection.setSSLSocketFactory(sslSocketFactory);
            return connection.getInputStream();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
    private String parseString(String s){
        StringBuilder tmp=new StringBuilder();
        for(int i=0;i<s.length()-1;i++){
            if ('A'<s.charAt(i)&&s.charAt(i)<'Z')
            {
                tmp.append("-");
                tmp.append(s.charAt(i)+('Z'-'A'));
            }
            else tmp.append(s.charAt(i));
        }
        return tmp.toString();
    }

    private HttpsURLConnection addHeader(HttpsURLConnection connection){
        try {
            connection.setRequestMethod(method);
            Map<String,String> header=makeHeader();
            for(String key:header.keySet()){
                connection.addRequestProperty(key,header.get(key));
            }
        } catch (ProtocolException e) {
            e.printStackTrace();
        }
        return connection;
    }

    class MyTrustManager implements X509TrustManager{
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

        }

        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }
    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public void setAcceptEncoding(String acceptEncoding) {
        this.acceptEncoding = acceptEncoding;
    }

    public void setAcceptLanguage(String acceptLanguage) {
        this.acceptLanguage = acceptLanguage;
    }

    public void setCacheControl(String cacheControl) {
        this.cacheControl = cacheControl;
    }

    public void setAccept(String accept) {
        this.accept = accept;
    }

    public void setCookie(String cookie) {
        this.cookie = cookie;
    }

}
